<?php
/**
* usermark.php
*
* Create on 2016-9-6
* Create by TT
*/

namespace haibao\user\data;

class UserMark extends \haibao\user\data\BaseMysql {

	public function __construct() {
		parent::__construct ( '\haibao\user\model\data\MarkImage' );
		$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
	}
	
	/**
	 * 添加图片收藏
	 * @param \haibao\user\model\data\markimage $model
	 */
	public function addMarkImage($model){
		$sql = 'select * from MarkImage where UserId=%s and ImageId=%s';
		$result = $this->query($sql,array($model->UserId,$model->ImageId));
		$row = $result->fetch_assoc();
		if(!$row){
		    $this->add($model);
		    //给图片加热度
		    $data = new \haibao\cms\data\ImageProperty();
		    $data->updateMarkNum($model->ImageId);
		}
	}
	/**
	 * 是否收藏图片
	 * @param \haibao\user\model\data\markimage $model
	 * @param unknown $imageIdArr
	 */
	public function hasMarkImage($imageIds,$userId){
	    if(is_array($imageIds)){
	        $str = preg_replace('/(\d+)/', '%s', implode(',', $imageIds));
	        $sql = 'SELECT * FROM MarkImage WHERE UserId='.$userId.' AND ImageId IN ('.$str.')';
	        $result = $this->query($sql,$imageIds);
	    }else{
	        $sql = 'SELECT * FROM MarkImage WHERE UserId = %s AND ImageId = %s';
	        $result = $this->query($sql,array($userId,$imageIds));
	    }
	    $temp = array();
	    while ($result->fetch_assoc()){
	        $temp[] = $result->getData('ImageId'); 
	    }
	    return $temp;
	}
	
	public function delMarkImages($userId,$imageIds){
		if(is_array($imageIds)){
			$str = preg_replace('/(\d+)/', '%s', implode(',', $imageIds));
			$sql = "delete from MarkImage where UserId=%s and ImageId in (".$str.")";
			$this->query($sql,array_merge(array($userId),$imageIds));
		} else {
			$sql = 'delete from MarkImage where UserId=%s and ImageId=%s';
			$this->query($sql,array($userId,$imageIds));
		}
	}
	
	public function updateMarkImageCatetory($userId,$categoryId,$imageIds){
		if(is_array($imageIds)){
			$str = preg_replace('/(\d+)/', '%s', implode(',', $imageIds));
			$sql = "update MarkImage set CategoryId=%s where UserId=%s and ImageId in (".$str.")";
			$this->query($sql,array_merge(array($categoryId,$userId),$imageIds));
			;
		} else {
			$sql = 'update MarkImage set CategoryId=%s where UserId=%s and ImageId=%s';
			$this->query($sql,array($categoryId,$userId,$imageIds));
		}
	}
	
	public function updateMarkImageDesc($userId,$imageId,$desc){
		$sql = 'update MarkImage set Description=%s where UserId=%s and ImageId=%s';
		$this->query($sql,array($desc,$userId,$imageId));
	}
	
	
	public function getMarkCategoryList($userId){
		$categoryList = array();
		$sql = 'select Id,CategoryName,Deletable from MarkCategory where UserId=%s';
		$result = $this->query($sql,array($userId));
		while ($result->fetch_assoc()){
			$categoryList[] = array('Id'=>$result->getData('Id'),'CategoryName'=>$result->getData('CategoryName'),'Deletable'=>$result->getData('Deletable'));
		}
		
		return $categoryList;
	}
	public function getUserMarkImageIds($userId){
		$categoryList = array();
		$sql = 'select Id,CategoryName,Deletable from MarkCategory where UserId=%s';
		$result = $this->query($sql,array($userId));
		while ($result->fetch_assoc()){
			$categoryList[] = array('Id'=>$result->getData('Id'),'CategoryName'=>$result->getData('CategoryName'),'Deletable'=>$result->getData('Deletable'));
		}

		return $categoryList;
	}

	public function getImageIdsByUserId($userId,$categoryId,$searchName){
		$parameters = array($userId);
		$imageIds = array();
		$search = '';
		if($categoryId >= 0){
			$search .= ' and CategoryId = %s';
			$parameters[] = $categoryId;
		}
		if($searchName){
			$search .= " and Description like %s ";
			$parameters[] = "%".$searchName."%";
		}
		$sql = "select ImageId from MarkImage where UserId = %s".$search." order by CreateTime desc";
		$result = $this->query($sql,$parameters);
		while($result->fetch_assoc()){
			$imageIds[] = $result->getData('ImageId');
		}
		return $imageIds;
	}

	public function getMarkImagesByImageIds($userId,$imageIds){
		$str = preg_replace('/(\d+)/', '%s', implode(',', $imageIds));
		$sql = "select * from MarkImage where UserId=%s and ImageId in (".$str.")";
		$result = $this->query($sql,array_merge(array($userId),$imageIds));
		$inArr = array();
		while($result->fetch_assoc()){
			array_push($inArr,$result->getData('ImageId'));
		}
		return $inArr;
	}
	
	public function havCategoryName($userId,$name,$id = null){
		$sqlVal = array($userId,$name);
		$sql = 'select Id from MarkCategory where UserId=%s and CategoryName=%s';
		if($id){
			$sql .= ' and Id<>%s';
			$sqlVal[] = $id;
		}
		$result = $this->query($sql,$sqlVal);
		$row = $result->fetch_assoc();
		if ($row){
			return true;
		}
		
		return false;
	}

	public function delMarkCategory($categoryId,$userId){
		$this->beginTranscation();
		try{
			$delSql = "delete from MarkCategory where Id = %s and UserId=%s";
			$this->query($delSql,array($categoryId,$userId));
			$updateSql = "update MarkImage set CategoryId=%s where CategoryId=%s and UserId=%s";
			$this->query($updateSql,array(\haibao\user\model\data\MarkCategory::DEFAULT_CATEGORY_ID,$categoryId,$userId));
		}catch(\Exception $e){
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}

	public function updateMarkCategory($categoryId,$categoryName,$userId){
		$sql = "update MarkCategory set CategoryName=%s where Id=%s and UserId=%s";
		$result = $this->query($sql,array($categoryName,$categoryId,$userId));
		return $result->getAffectRow();
	}

	public function manageCategory($models,$updateCates,$delIds,$userId){
		$this->beginTranscation();
		try{
			$this->addAll($models);
			foreach($updateCates as $cate){
				$sql = "update MarkCategory set CategoryName=%s where Id=%s and UserId = %s";
				$this->query($sql,array($cate[1],$cate[0],$userId));
			}
			if($delIds){
				$str = preg_replace('/(\d+)/', '%s', implode(',', $delIds));
				$sql = "delete from MarkCategory where Id in (".$str.") and UserId = %s";
				$manageImageSql = "update MarkImage set CategoryId = ".\haibao\user\model\data\MarkCategory::DEFAULT_CATEGORY_ID." where CategoryId in (".$str.") and UserId = %s";
				array_push($delIds,$userId);
				$this->query($sql,$delIds);
				$this->query($manageImageSql,$delIds);
			}
		}catch(\Exception $e){
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}


}